//
//  DataCanlenderViewController.m
//  ZeroFashion
//
//  Created by 孙琪 on 16/2/29.
//  Copyright © 2016年 apple. All rights reserved.
//

/// 每月的第一天是周几
#define  zhouji [Tool getWeekOfFirstDayOfMonth:self.CurrentYear withMonth:self.CurrentMonth]
/// 某个月共所少天
#define allDays [Tool getDaysOfMonth:self.CurrentYear withMonth:self.CurrentMonth]
/// 某个月共需建需要多少行(包括周几表头)
#define  hang ((allDays+zhouji)/7+2)
// 字体
#define GETFONTSIZE(fontSize) (HEIGHT/667*fontSize)
#define GETSIZE(num)          (WIDTH/375*num)
#define GETHEIGHT(num)        (HEIGHT/667*num)


// 宽／高
#define WIDTH     [[UIScreen mainScreen]bounds].size.width
#define HEIGHT    [[UIScreen mainScreen]bounds].size.height


#import "DataCanlenderViewController.h"
#import "Tool.h"
#import "mb2ViewAdditions.h"

@interface DataCanlenderViewController ()

@property (nonatomic, assign) int CurrentYear;  //当前年份
@property (nonatomic, assign) int CurrentMonth; //当前月
@property (nonatomic, assign) int yearStr;  //年
@property (nonatomic, assign) int monthStr; //月
@property (nonatomic, assign) int clickNumber;//纪录点击button次数
@property (nonatomic, assign) int count;//每次精确的行数

@property (nonatomic, strong) UIView * clearV;//透明视图
@property (nonatomic, strong) UIView * hV;//横线
@property (nonatomic, strong) UIView * sV;//竖线
@property (nonatomic, strong) UILabel * myTitle;//显示年月标头
@property (nonatomic, strong) UIButton * pyBtn;//每次点击的button
@property (nonatomic, strong) UIButton * lastBtn;//上一个月按钮
@property (nonatomic, strong) UIButton * nextBtn;//下一个月按钮
@property (nonatomic, strong) UIButton * dayBtn;

@property (nonatomic,   copy) NSString * proId;//每个搭配图的ID
@property (nonatomic,   copy) NSString * dayStr;
@property (nonatomic,   copy) NSString * iconPath;
@property (nonatomic, strong) NSDictionary * sjDic;//数据字典

@property (nonatomic, strong) UIVisualEffectView * detailView;//点击展开视图
@property (nonatomic, strong) UIImageView * bigImgV;//展示图中的大图
@property (nonatomic, strong) UIImageView * btnV;//btn上的颜色view

@property (nonatomic, strong) NSMutableArray * dicArry;//数据数组
@property (nonatomic, strong) NSMutableArray * dayBtnTagArr;
@property (nonatomic, strong) NSMutableArray * timeStrArr;//数据日期数组
@property (nonatomic, strong) NSMutableArray * idArry;//ID数组
@property (nonatomic, strong) NSMutableArray * didClickBtnArry;//可以点击的btn数组
@property (nonatomic, strong) NSMutableArray * smallVArry;//展示四张小视图
@property (nonatomic, strong) NSMutableArray * hVArray;//横线数组，方便下移
@property (nonatomic, strong) NSMutableArray * sVArray;//竖线数组

@end

@implementation DataCanlenderViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    //背景
    self.view.backgroundColor = [UIColor darkGrayColor];
  
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:GETFONTSIZE(20)],
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    //表头
    [self creatCanlenderHeader];
    //创建日历
    [self creatCutLineAndBtn];
}

#pragma mark-懒加载
//一个月内所有的btn数组
- (NSMutableArray *)dayBtnTagArr{
    if (!_dayBtnTagArr) {
        _dayBtnTagArr =[NSMutableArray array];
    }    
    return _dayBtnTagArr;
}



#pragma mark-年月日表头
- (void)creatCanlenderHeader{
    
    self.myTitle = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH/2-GETSIZE(60), GETSIZE(90),GETSIZE(120), GETSIZE(25))];
    self.myTitle.textAlignment = NSTextAlignmentCenter;
    self.myTitle.textColor = [UIColor whiteColor];
    self.myTitle.font = [UIFont systemFontOfSize:GETSIZE(18)];
    [self.view  addSubview:_myTitle];
    
    _lastBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _lastBtn.frame = CGRectMake(_myTitle.left-GETSIZE(40), GETSIZE(75), GETSIZE(50), GETSIZE(55));
    [_lastBtn setImage:[UIImage imageNamed:@"l"] forState:UIControlStateNormal];
    _lastBtn.backgroundColor = [UIColor redColor];
    [_lastBtn addTarget:self action:@selector(didLastBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_lastBtn];
    
    _nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _nextBtn.frame = CGRectMake(_myTitle.right, GETSIZE(75), GETSIZE(50), GETSIZE(53));
    [_nextBtn setImage:[UIImage imageNamed:@"r"] forState:UIControlStateNormal];
    _nextBtn.backgroundColor = [UIColor redColor];
    [_nextBtn addTarget:self action:@selector(didNextBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_nextBtn];
    
    self.CurrentYear =[Tool getYear];     //当前年
    self.CurrentMonth = [Tool getMonth];   //当前月
    _myTitle.text =[NSString stringWithFormat:@"%d年%d月",self.CurrentYear,self.CurrentMonth];
}


#pragma mark- 上下月标头切换点击事件
//上一个月
- (void)didLastBtn{
    
    if (self.CurrentMonth > 1) {
        self.CurrentMonth -= 1;
        [self refreachMonthDayBtn];
    } else {
        self.CurrentMonth = 12;
        self.CurrentYear -= 1;
        [self refreachMonthDayBtn];
    }
}


// 下一个月
- (void)didNextBtn{
    
    if (self.CurrentMonth < 12) {
        self.CurrentMonth += 1;
        [self refreachMonthDayBtn];
    }else{
        
        self.CurrentMonth = 1;
        self.CurrentYear = self.CurrentYear + 1;
        [self refreachMonthDayBtn];
    }
}



#pragma mark-刷新上下月份

- (void)refreachMonthDayBtn{
    self.myTitle.text =[NSString stringWithFormat:@"%d年%d月",self.CurrentYear,self.CurrentMonth];
    if (_clearV) {
        [_clearV removeFromSuperview];
    }
    [self creatCutLineAndBtn];
    
}



#pragma mark-循环创建日历按钮 分割线
- (void)creatCutLineAndBtn{
    ///每次创建之前清空数组
    [self.dayBtnTagArr removeAllObjects];
    
    self.clearV = [[UIView alloc]init];
    _clearV.frame = CGRectMake(0, GETSIZE(120), WIDTH, GETSIZE(520));
    _clearV.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_clearV];
    
    ///精确行数
    self.count = 0;
    if ((allDays+zhouji)%7 == 0) {
        _count = hang-1;
    }else{
        _count = hang;
    }
    
    //竖线
    self.sVArray = [NSMutableArray array];
    for (int i = 0; i<8; i++) {
        self.sV= [[UIView alloc]init];
        _sV.frame = CGRectMake(GETSIZE(30)+i*GETSIZE(45),GETSIZE(20),GETSIZE(1), (_count+1)*GETSIZE(45));
        _sV.backgroundColor = [UIColor colorWithWhite:1 alpha:0.1];
        [_sVArray addObject:_sV];
        [_clearV addSubview:_sV];
    }
    
    //横线
    self.hVArray = [NSMutableArray array];
    /// 周几标头，每次多建一行(线比行多一)
    for (int i = 0; i<_count+1; i++) {
        self.hV = [[UIView alloc]init];
        _hV.frame = CGRectMake(GETSIZE(5), GETSIZE(40)+i*GETSIZE(45), WIDTH-GETSIZE(10), GETSIZE(1));
        _hV.backgroundColor = [UIColor colorWithWhite:1 alpha:0.1];
        [_hVArray addObject:_hV];
        [_clearV addSubview:_hV];
    }
    
    ///创建button
    NSArray * zhouArry = @[@"日",@"一",@"二",@"三",@"四",@"五",@"六"];
    //创建周lable
    for (int i=0; i<7; i++) {
        UILabel * zhouLbl = [[UILabel alloc]init];
        zhouLbl.frame =CGRectMake(GETSIZE(30) + i * GETSIZE(45), GETSIZE(40), GETSIZE(45) , GETSIZE(45));
        zhouLbl.text = zhouArry[i];
        zhouLbl.textColor = [UIColor colorWithWhite:1 alpha:0.7];
        zhouLbl.textAlignment = NSTextAlignmentCenter;
        [_clearV addSubview:zhouLbl];
    }
    
    //创建dayButton
    ///把最后一行所剩下的空白格也建成btn，好让其位置下移
    for (int i = 0; i<(_count-1)*7-zhouji; i++) {
        int iXPos = (i+zhouji)%7;
        int iYPos = (i+zhouji)/7;
        self.dayBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        _dayBtn.frame =CGRectMake(GETSIZE(30) + iXPos * GETSIZE(45), GETSIZE(45) * iYPos+GETSIZE(40)+GETSIZE(45), GETSIZE(45) , GETSIZE(45));
      
        _dayBtn.tag = 100+i;
        
        [_dayBtn addTarget:self action:@selector(touchBtn:) forControlEvents:UIControlEventTouchUpInside];
      
        if (i<allDays) {
            [_dayBtn setTitle:[NSString stringWithFormat:@"%d",i+1] forState:UIControlStateNormal];
        }
        
        
        [_dayBtn setTitleColor:[UIColor colorWithWhite:1 alpha:0.7] forState:UIControlStateNormal];
        [self.dayBtnTagArr addObject:self.dayBtn];
        [_clearV addSubview:_dayBtn];
        
        //创建展示view
        self.detailView = [[UIVisualEffectView alloc]initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]];
        int y = (int) (self.pyBtn.tag-100+zhouji)/7;
        _detailView.frame = CGRectMake(0, (y+2)*GETSIZE(45)+GETSIZE(40), _clearV.width, 0);
        [_clearV addSubview:_detailView];
        
    }
}


#pragma mark-dayBtn点击事件
- (void)touchBtn:(UIButton *)sender{
    __weak __typeof(self)weakSelf = self;
    if (sender.tag == _pyBtn.tag) {
        _clickNumber++;
        if (_clickNumber%2==1) {
            ///1是下拉，0是上移
            [self buttonFallingWithButton:sender Move:0];
            
            [UIView animateWithDuration:0.2 animations:^{
                [weakSelf.bigImgV setHeight:0];
                
                for (UIView * smallV in weakSelf.smallVArry) {
                    [smallV setHeight:0];
                }
            }];
            
            
            [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:1 initialSpringVelocity:0 options: 0 animations:^{
                
                for (UIButton * btn in weakSelf.dayBtnTagArr) {
                    if (btn.tag == weakSelf.pyBtn.tag) {
                        btn.backgroundColor = [UIColor clearColor];
                    }
                }
                
                
                //距上有多少行
                int y = ([weakSelf.pyBtn.titleLabel.text intValue]+zhouji)/7;
                if (([weakSelf.pyBtn.titleLabel.text intValue]+zhouji)%7==0) {
                    y=y+1;
                }else{
                    y=y+2;
                }
                // 改变动画位置
                weakSelf.detailView.frame = CGRectMake(0, y*GETSIZE(45)+GETSIZE(40), weakSelf.clearV.width, 0);
                
            } completion:^(BOOL finished) {
                
                [weakSelf.bigImgV removeFromSuperview];
                for (UIView * smallV in weakSelf.smallVArry) {
                    [smallV removeFromSuperview];
                }
                
            }];
            
            // NSLog(@"点击以上次一样，应该上合");
            
        }else{
            
            [self buttonFallingWithButton:sender Move:1];
            [UIView animateWithDuration:0.5 animations:^{
                for (UIButton * btn in weakSelf.dayBtnTagArr) {
                    if (btn.tag == weakSelf.pyBtn.tag) {
                        btn.backgroundColor = [UIColor colorWithWhite:1 alpha:0.3];
                    }
                }
                // 改变动画位置
                [weakSelf.detailView setHeight:GETSIZE(153)];
            }];
            
            [self creatbigImgV];
            [self creatFourSmallImgs];
            //          NSLog(@"与上次一样，应该下拉");
        }
        
    }else{
        //            NSLog(@"与上次不同，应该下拉");
        
        ///删除clearView，重新添加
        if(weakSelf.clearV){
            [weakSelf.clearV removeFromSuperview];
        }
        weakSelf.pyBtn = sender;
        [self creatCutLineAndBtn];
        [self buttonFallingWithButton:sender Move:1];
        [UIView animateWithDuration:0.5 animations:^{
            
            for (UIButton * btn in weakSelf.dayBtnTagArr) {
                if (btn.tag == weakSelf.pyBtn.tag) {
                    btn.backgroundColor = [UIColor colorWithWhite:1 alpha:0.3];
                }
            }
            
            // 改变动画位置
            [weakSelf.detailView setHeight:GETSIZE(153)];
       
        }];
        
        weakSelf.clickNumber = 0;
    
        
        [self creatbigImgV];
        [self creatFourSmallImgs];
    }
}


#pragma mark-btn下移上移
- (void)buttonFallingWithButton:(UIButton *)btn Move:(int)move{
    __weak __typeof(self)weakSelf = self;
    // 相关日期计算
    int day = (int)btn.tag - 100 +1;
    
    int haisheng = 7 - ((day - (7- zhouji))%7);
    
    if (day <= (7-zhouji)) {
        
        haisheng = 7-zhouji-day;
    }
    
    if (((day - (7- zhouji))%7)==0) {
        
        haisheng = 0;
    }
    
    // 从第几个button开始下移 的tag值
    int py = day + haisheng + 100-1;
    
    ///横线 竖线 下移上移动画
    [UIView animateWithDuration:0.5 animations:^{
        if (move == 1) {
            for (UIView * ssv in weakSelf.sVArray) {
                ssv.frame = CGRectMake(ssv.origin.x, ssv.origin.y, ssv.width, ssv.height+GETSIZE(153));
            }
            
            for (UIView * hhv in weakSelf.hVArray) {
                if (hhv.origin.y>((py-100)/7+2)*GETSIZE(45)+GETSIZE(20)) {
                    hhv.frame = CGRectMake(hhv.origin.x, hhv.origin.y+GETSIZE(153), hhv.width, hhv.height);
                }
            }
            
            for (UIButton  * button in  self.clearV.subviews) {
                
                if (button.tag > py) {
                    
                    button.frame = CGRectMake(button.origin.x, button.origin.y + GETSIZE(153), button.size.width, button.size.height);
                }
            }
            
        }
        
        if (move == 0){
            for (UIView * ssv in weakSelf.sVArray) {
                ssv.frame = CGRectMake(ssv.origin.x, ssv.origin.y, ssv.width, ssv.height-GETSIZE(153));
            }
            
            for (UIView * hhv in weakSelf.hVArray) {
                if (hhv.origin.y>((py-100)/7+2)*GETSIZE(45)+GETSIZE(20)) {
                    hhv.frame = CGRectMake(hhv.origin.x, hhv.origin.y-GETSIZE(153), hhv.width, hhv.height);
                }
            }
            
            for (UIButton  * button in  self.clearV.subviews) {
                if (button.tag > py) {
                    button.frame = CGRectMake(button.origin.x, button.origin.y-GETSIZE(153), button.size.width, button.size.height);
                }
            }
        }
    }];
}


#pragma mark-添加第一张大图

- (void)creatbigImgV{
    __weak __typeof(self)weakSelf = self;
    // 大图
    self.bigImgV = [[UIImageView alloc]init];
    _bigImgV.image = [UIImage imageNamed:@"zt"];
    _bigImgV.backgroundColor = [UIColor cyanColor];
    _bigImgV.frame = CGRectMake(WIDTH*2, GETSIZE(8), WIDTH/2-GETSIZE(50), WIDTH/2-GETSIZE(50));
    _bigImgV.layer.shadowColor = [UIColor darkGrayColor].CGColor;//阴影颜色
    _bigImgV.layer.shadowOffset = CGSizeMake(2, 2);//偏移距离
    _bigImgV.layer.shadowOpacity = 0.7;//不透明度
    _bigImgV.layer.shadowRadius = GETSIZE(1.0);//半径
    [self.view addSubview:_bigImgV];
    
    [UIView animateWithDuration:0.3 delay:0.3 usingSpringWithDamping:1 initialSpringVelocity:0 options: 0 animations:^{
        
        // 改变动画位置
        weakSelf.bigImgV.frame = CGRectMake(GETSIZE(40),GETSIZE(8), WIDTH/2-GETSIZE(50), WIDTH/2-GETSIZE(50));
        [weakSelf.detailView.contentView addSubview:weakSelf.bigImgV];
        
    } completion:^(BOOL finished) {
        
    }];
}



#pragma mark-依次添加四张小图
- (void)creatFourSmallImgs{
    __weak __typeof(self)weakSelf = self;
     for (int i = 0; i<4; i++) {
        
        NSInteger WImg = _bigImgV.width/2-GETSIZE(5);
        NSInteger HImg = _bigImgV.width/2-GETSIZE(5);
        NSInteger XImg = i%2 * (WImg+GETSIZE(10)) + GETSIZE(20) + _bigImgV.right;
        NSInteger YImg = i/2 * (HImg + GETSIZE(8))+GETSIZE(10);
        
        UIImageView * smallV = [[UIImageView alloc]init];
        smallV.backgroundColor = [UIColor whiteColor];
        smallV.frame = CGRectMake(2*WIDTH, YImg,WImg, HImg);
        //显示图片一部分
        [smallV setContentScaleFactor:[[UIScreen mainScreen] scale]];
         smallV.contentMode =  UIViewContentModeScaleAspectFill;
         smallV.autoresizingMask = UIViewAutoresizingFlexibleHeight;
         smallV.clipsToBounds  = YES;

         NSString * imgName = [NSString stringWithFormat:@"ft%d",i+1];
         smallV.image = [UIImage imageNamed:imgName];

        smallV.layer.shadowColor = [UIColor darkGrayColor].CGColor;//阴影颜色
        smallV.layer.shadowOffset = CGSizeMake(GETSIZE(2), GETSIZE(2));//偏移距离
        smallV.layer.shadowOpacity = 0.7;//不透明度
        smallV.layer.shadowRadius = GETSIZE(1.0);//半径
        
        smallV.tag = 100000+i;
        
        [self.smallVArry addObject:smallV];
        
        if (smallV.tag == 100000) {
            [UIView animateWithDuration:0.2 delay:0.5
                 usingSpringWithDamping:1 initialSpringVelocity:0 options: 0 animations:^{
                   
                     //改变动画位置
                     smallV.frame = CGRectMake(XImg, YImg,WImg, HImg);
                     [weakSelf.detailView.contentView  addSubview:smallV];
                     
                 } completion:^(BOOL finished) {
                     
            }];
        }
        
        
        if (smallV.tag == 100001) {
            [UIView animateWithDuration:0.2 delay:0.6
                 usingSpringWithDamping:1 initialSpringVelocity:0 options: 0 animations:^{
                     //改变动画位置
                     smallV.frame = CGRectMake(XImg, YImg,WImg, HImg);
                     [weakSelf.detailView.contentView  addSubview:smallV];
                     
                 } completion:^(BOOL finished) {
                     
                 }];
        }
        
        
        if (smallV.tag == 100002) {
            [UIView animateWithDuration:0.2 delay:0.7
                 usingSpringWithDamping:1 initialSpringVelocity:0 options: 0 animations:^{
                     //改变动画位置
                     smallV.frame = CGRectMake(XImg, YImg,WImg, HImg);
                     [weakSelf.detailView.contentView  addSubview:smallV];
                     
                 } completion:^(BOOL finished) {
                     
            }];
        }
        
        if (smallV.tag == 100003) {
            [UIView animateWithDuration:0.2 delay:0.8 usingSpringWithDamping:1 initialSpringVelocity:0 options: 0 animations:^{
                //改变动画位置
                smallV.frame = CGRectMake(XImg, YImg,WImg, HImg);
                [weakSelf.detailView.contentView  addSubview:smallV];
                
            } completion:^(BOOL finished) {
                
          }];
        }
     }
}
@end
